home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / print-sl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-07  |  5.3 KB  |  247 lines

  1. /*
  2.  * Copyright (c) 1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  */
  21.  
  22. #ifndef lint
  23. static  char rcsid[] =
  24.     "@(#)$Header: print-sl.c,v 1.17 91/10/07 20:18:35 leres Exp $ (LBL)";
  25. #endif
  26.  
  27. #ifdef CSLIP
  28. #include <stdio.h>
  29. #include <netdb.h>
  30. #include <ctype.h>
  31. #include <signal.h>
  32. #include <errno.h>
  33. #include <sys/param.h>
  34. #include <sys/types.h>
  35. #include <sys/time.h>
  36. #include <sys/timeb.h>
  37. #include <sys/socket.h>
  38. #include <sys/file.h>
  39. #include <sys/mbuf.h>
  40. #include <sys/ioctl.h>
  41.  
  42. #include <net/if.h>
  43. #include <netinet/in.h>
  44. #include <netinet/in_systm.h>
  45. #include <netinet/ip.h>
  46. #include <netinet/if_ether.h>
  47. #include <netinet/ip_var.h>
  48. #include <netinet/udp.h>
  49. #include <netinet/udp_var.h>
  50. #include <netinet/tcp.h>
  51. #include <netinet/tcpip.h>
  52.  
  53. #include <net/slcompress.h>
  54. #include <net/slip.h>
  55. #include <net/bpf.h>
  56.  
  57. #include "interface.h"
  58. #include "addrtoname.h"
  59.  
  60. static int lastlen[2][256];
  61. static int lastconn = 255;
  62.  
  63. static void compressed_sl_print();
  64.  
  65. void
  66. sl_if_print(p, tvp, length, caplen)
  67.     u_char *p;
  68.     struct timeval *tvp;
  69.     int length;
  70.     int caplen;
  71. {
  72.     struct ip *ip;
  73.  
  74.     ts_print(tvp);
  75.  
  76.     if (caplen < SLIP_HDRLEN) {
  77.         printf("[|slip]");
  78.         goto out;
  79.     }
  80.     /*
  81.      * Some printers want to get back at the link level addresses,
  82.      * and/or check that they're not walking off the end of the packet.
  83.      * Rather than pass them all the way down, we set these globals.
  84.      */
  85.     packetp = (u_char *)p;
  86.     snapend = (u_char *)p + caplen;
  87.  
  88.     length -= SLIP_HDRLEN;
  89.  
  90.     ip = (struct ip *)(p + SLIP_HDRLEN);
  91.  
  92.     if (eflag)
  93.         sliplink_print(p, ip, length);
  94.  
  95.     ip_print(ip, length);
  96.  
  97.     if (xflag)
  98.         default_print((u_short *)ip, caplen - SLIP_HDRLEN);
  99.  out:
  100.     putchar('\n');
  101. }
  102.  
  103. sliplink_print(p, ip, length)
  104.     u_char *p;
  105.     struct ip *ip;
  106.     int length;
  107. {
  108.     int dir;
  109.     int hlen;
  110.  
  111.     dir = p[SLX_DIR];
  112.     putchar(dir == SLIPDIR_IN ? 'I' : 'O');
  113.     putchar(' ');
  114.  
  115.     if (nflag) {
  116.         /* XXX just dump the header */
  117.         int i;
  118.  
  119.         for (i = 0; i < 15; ++i)
  120.             printf("%02x.", p[SLX_CHDR + i]);
  121.         printf("%02x: ", p[SLX_CHDR + 15]);
  122.         return;
  123.     }
  124.     switch (p[SLX_CHDR] & 0xf0) {
  125.  
  126.     case TYPE_IP:
  127.         printf("ip %d: ", length + SLIP_HDRLEN);
  128.         break;
  129.  
  130.     case TYPE_UNCOMPRESSED_TCP:
  131.         /*
  132.          * The connection id is stode in the IP protcol field.
  133.          */
  134.         lastconn = ip->ip_p;
  135.         hlen = ip->ip_hl;
  136.         hlen += ((struct tcphdr *)&((int *)ip)[hlen])->th_off;
  137.         lastlen[dir][lastconn] = length - (hlen << 2);
  138.         printf("utcp %d: ", lastconn);
  139.         break;
  140.  
  141.     default:
  142.         if (p[SLX_CHDR] & TYPE_COMPRESSED_TCP) {
  143.             compressed_sl_print(&p[SLX_CHDR], ip, length, dir);
  144.             printf(": ");
  145.         } else
  146.             printf("slip-%d!: ", p[SLX_CHDR]);
  147.     }
  148. }
  149.  
  150. static u_char *
  151. print_sl_change(str, cp)
  152.     char *str;
  153.     register u_char *cp;
  154. {
  155.     register u_int i;
  156.  
  157.     if ((i = *cp++) == 0) {
  158.         i = (cp[0] << 8) | cp[1];
  159.         cp += 2;
  160.     }
  161.     printf(" %s%d", str, i);
  162.     return (cp);
  163. }
  164.  
  165. static u_char *
  166. print_sl_winchange(cp)
  167.     register u_char *cp;
  168. {
  169.     register short i;
  170.  
  171.     if ((i = *cp++) == 0) {
  172.         i = (cp[0] << 8) | cp[1];
  173.         cp += 2;
  174.     }
  175.     if (i >= 0)
  176.         printf(" W+%d", i);
  177.     else
  178.         printf(" W%d", i);
  179.     return (cp);
  180. }
  181.  
  182. static void
  183. compressed_sl_print(chdr, ip, length, dir)
  184.     u_char *chdr;
  185.     int length;
  186.     struct ip *ip;
  187.     int dir;
  188. {
  189.     register u_char *cp = chdr;
  190.     register u_int flags;
  191.     int hlen;
  192.     
  193.     flags = *cp++;
  194.     if (flags & NEW_C) {
  195.         lastconn = *cp++;
  196.         printf("ctcp %d", lastconn);
  197.     } else
  198.         printf("ctcp *");
  199.  
  200.     /* skip tcp checksum */
  201.     cp += 2;
  202.  
  203.     switch (flags & SPECIALS_MASK) {
  204.     case SPECIAL_I:
  205.         printf(" *SA+%d", lastlen[dir][lastconn]);
  206.         break;
  207.  
  208.     case SPECIAL_D:
  209.         printf(" *S+%d", lastlen[dir][lastconn]);
  210.         break;
  211.  
  212.     default:
  213.         if (flags & NEW_U)
  214.             cp = print_sl_change("U=", cp);
  215.         if (flags & NEW_W)
  216.             cp = print_sl_winchange(cp);
  217.         if (flags & NEW_A)
  218.             cp = print_sl_change("A+", cp);
  219.         if (flags & NEW_S)
  220.             cp = print_sl_change("S+", cp);
  221.         break;
  222.     }
  223.     if (flags & NEW_I)
  224.         cp = print_sl_change("I+", cp);
  225.  
  226.     /*
  227.      * 'hlen' is the length of the uncompressed TCP/IP header (in longs).
  228.      * 'cp - chdr' is the length of the compressed header.
  229.      * 'length - hlen' is the amount of data in the packet.
  230.      */
  231.     hlen = ip->ip_hl;
  232.     hlen += ((struct tcphdr *)&((long *)ip)[hlen])->th_off;
  233.     lastlen[dir][lastconn] = length - (hlen << 2);
  234.     printf(" %d (%d)", lastlen[dir][lastconn], cp - chdr);
  235. }
  236. #else
  237. #include <stdio.h>
  238. void
  239. sl_if_print()
  240. {
  241.     void error();
  242.  
  243.     error("not configured for slip");
  244.     /* NOTREACHED */
  245. }
  246. #endif
  247.